package com.noovo.comport.remoteserver;

import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import java.util.HashMap;

import com.noovo.comport.serial.MyFunc;
import com.noovo.comport.serial.SerialBean;
import com.noovo.comport.server.ComInfo;
import com.noovo.comport.aidl.*;

public class RemoteComService extends Service {
	private static RemoteComService service;
	private IComCallback mRemoteCallback;
	private HashMap<Integer, RemoteComHandler> MAP_HADNLER;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ComInfo.HANDLER_SERVICE_RECEIVE: {
				ContentValues values = (ContentValues) msg.obj;
				int port = values.getAsInteger(ComInfo.DB_ITEM_PORT);
				String comName = values.getAsString(ComInfo.DB_ITEM_NAME);
				byte[] m = values.getAsByteArray(ComInfo.DB_ITEM_RECEIVE);
				if (ComInfo.DEBUG_ON)
					Log.d(ComInfo.TAG, "COM OUT RECEIVE://port=" + port + " portName=" + comName + " msg-length="
							+ m.length + " " + MyFunc.ByteArrToHex(m));
				if (mRemoteCallback != null) {
					try {
						mRemoteCallback.onComCallback(comName, m);
					} catch (RemoteException e) {
						e.printStackTrace();
					}
				}
			}
				break;
			case ComInfo.HANDLER_SERVICE_ERROR: {
				int port = (Integer) msg.obj;
				MAP_HADNLER.get(port).initAndOpenSerial();
			}
				break;
			case ComInfo.HANDLER_SERVICE_COM_DATA_RECEIVE: {
				SerialBean bean = (SerialBean) msg.obj;
				MAP_HADNLER.get(bean.getPort()).onDataReceive(bean.getBuffer());
			}
				break;
			default:
				MAP_HADNLER.get(msg.what).finishDataReceive();
				break;
			}
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return new IRemoteService();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(ComInfo.TAG, "ComServer onCreate");
		MAP_HADNLER = new HashMap<Integer, RemoteComHandler>();
		for (int port = 1; port < 5; port++) {
			RemoteComHandler comHandler = new RemoteComHandler(port, mHandler);
			MAP_HADNLER.put(port, comHandler);
		}
	}

	@Override
	public void onDestroy() {
		for (int port = 1; port < 5; port++) {
			MAP_HADNLER.get(port).onClose();
		}
		mHandler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		flags = START_STICKY;
		return super.onStartCommand(intent, flags, startId);
	}

	public class IRemoteService extends IComService.Stub {

		@Override
		public void setCom(int port, String comName, int baudrate, int databits, int stopbits, char parity)
				throws RemoteException {
			Log.d(ComInfo.TAG, "COM IN SET://port=" + port + " portName=" + comName + " baudrate=" + baudrate
					+ " databits=" + databits + " stopbits=" + stopbits + " parity=" + parity);
			RemoteComHandler comHandler = MAP_HADNLER.get(port);
			comHandler.setName(comName);
			comHandler.onOpen(baudrate, databits, stopbits, parity);
		}

		@Override
		public void sendMessageOnce(int port, byte[] message) throws RemoteException {
			Log.d(ComInfo.TAG, "COM IN SEND://port=" + port + " msg-length=" + message.length + " loop=" + false
					+ MyFunc.ByteArrToHex(message));
			RemoteComHandler comHandler = MAP_HADNLER.get(port);
			comHandler.onSendMessage(message, false, 0);
		}

		@Override
		public void sendMessageLoop(int port, byte[] message, int delay) throws RemoteException {
			Log.d(ComInfo.TAG, "COM IN SEND://port=" + port + " msg-length=" + message.length + " loop=" + true
					+ " delay=" + delay + " " + MyFunc.ByteArrToHex(message));
			RemoteComHandler comHandler = MAP_HADNLER.get(port);
			comHandler.onSendMessage(message, true, delay);
		}

		@Override
		public void stopSendMessage(int port) throws RemoteException {
			Log.d(ComInfo.TAG, "COM IN SEND://port=" + port + " send close");
			RemoteComHandler comHandler = MAP_HADNLER.get(port);
			comHandler.onStopMessage();
		}

		@Override
		public void registerComCallback(IComCallback callback) throws RemoteException {
			mRemoteCallback = callback;
		}

		@Override
		public void unregisterComCallback() throws RemoteException {
			mRemoteCallback = null;
		}
	}
}
